<template>
	<view class="labels-list-wrap">
		<view class="labels-list">
			<view class="list-item" v-for="(item,index) in userLabels" :key="index"
				:class="{'nbr': (index + 1) % 4 == 0}" @tap.stop="selectLabel(item)">
				<image class="item-img" :src="getImgUrl(item.fileUrl)"></image>
				<view class="select-icon">
					<image class="select-icon-img" v-show="selectedLabels.indexOf(item.fileUrl) > -1" src="../../static/labelSelectedIcon.png"></image>
				</view>
			</view>
		</view>

		<view class="submit-btn-wrap">
			<largeBtn @btnClk="confirm">确定</largeBtn>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from "vuex"
	import areaData from "../../config/areaData.js"
	export default {
		data() {
			return {
				selectedLabels: []
			}
		},
		computed: {
			...mapState(['userLabels', 'selectedUserLabels']),
		},
		created() {
	        this.selectedUserLabels.forEach((url)=>{
				this.selectedLabels.push(url)
			})
		},

		methods: {
			selectLabel(item) {
				const index = this.selectedLabels.indexOf(item.fileUrl)
				if(index == -1){
					if(this.selectedLabels.length >= 5){
						uni.showToast({
							title: '最多只能选5个喜好',
							icon: 'none'
						})
					}else{
						this.selectedLabels.push(item.fileUrl)
					}
				}else{
					this.selectedLabels.splice(index,1)
				}
			},
			confirm(){
				this.$store.commit('setSelectedUserLabels',this.selectedLabels)
				uni.navigateBack()
			}
		}
	}
</script>

<style>
	page {
		background-color: #F3F7FD;
	}
</style>
<style lang="less">
	.labels-list-wrap {
		padding-bottom: 140rpx;
	}

	.labels-list {
		display: flex;
		flex-wrap: wrap;

		.list-item {
			position: relative;
			box-sizing: border-box;
			width: 25%;
			height: 187.5rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			border: 1rpx solid #ccc;
			border-top: none;
			border-left: none;

			&.nbr {
				border-right: none;
			}
		}

		.item-img {
			width: 120rpx;
			height: 120rpx;
		}

		.select-icon {
			position: absolute;
			width: 40rpx;
			height: 40rpx;
			right: 10rpx;
			top: 10rpx;
			border: 1px solid #eee;
			background-color: #fff;
			border-radius: 50%;

			.select-icon-img {
				width: 40rpx;
				height: 40rpx;
			}
		}
	}




	.submit-btn-wrap {
		position: fixed;
		z-index: 3;
		left: 40rpx;
		bottom: 48rpx;
		width: 670rpx;
	}
</style>
